<?php

namespace App\Admin\Extensions;

use App\Service\Order;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithMapping;
use App\Util\Url;

//新增两个 use
use Maatwebsite\Excel\Concerns\WithDrawings;
use Maatwebsite\Excel\Events\AfterSheet;
use Maatwebsite\Excel\Sheet;
use PhpOffice\PhpSpreadsheet\Worksheet\BaseDrawing;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;

class ExcelExpoter extends ExcelExporter implements WithDrawings
{

    protected $fileName = '商品订单列表.xlsx';

    public function __construct(Grid $grid = null)
    {
        Sheet::macro('styleCells', function (Sheet $sheet, string $cellRange, array $style) {
            $sheet->getDelegate()->getStyle($cellRange)->applyFromArray($style);
        });
        parent::__construct($grid);
    }



    protected $columns = [
        "id" => "ID",
        "order_goods.goods_name" => "商品名称",
        "order_goods.goods_img" => "商品图片",
        "sn" => "订单编号",
        "order_source" => "商品类型",
        "distributor.name" => "所属供应商",
        "work.work_name" => "所属医护工作室",
        "order_goods.product_standard" => "商品规格",
        "order_goods.product_remark" => "商品备注",
        "orderaddress.name" => "收货人姓名",
        "orderaddress.phone" => "联系方式",
        "orderaddress.detail" => "地址",
        "nums" => "商品数量",
        "created_at" => "创建时间",
        "pay_time" => "付款时间",
        "order_status" => "订单状态",
        "total_price" => "价格",
    ];


    public function drawings()
    {

//        dd($this->getData());
//        dd($this->getData());
        $draw_arr = [1 => 'upload/images/4c528ac2743e50f52c9d27f03f716c3b.jpg', 2 => 'upload/images/4c528ac2743e50f52c9d27f03f716c3b.jpg'];
        $result = [];
        $row = 1;
        foreach ($this->getData() as $k => $v) {
//            dd($v['order_goods']['goods_img']);
//            ${'drawing'.($k+1)} = new Drawing();
//            ${'drawing'.($k+1)}->setName('Other image');
//            ${'drawing'.($k+1)}->setDescription('This is a second image');
//            //图片路径
//            ${'drawing'.($k+1)}->setPath(public_path($v));
//            ${'drawing'.($k+1)}->setHeight(100);
//            //设置图片列
//            ${'drawing'.($k+1)}->setCoordinates('C'.($k+1));
//            $result[] = ${'drawing'.($k+1)};
//            $drawing = new Drawing();
//            $drawing->setPath(public_path('upload/'.$v['order_goods']['goods_img']));
//            $drawing->setCoordinates('C' . $row);
//            $drawing->setOffsetX(20);
//            $drawing->setOffsetY(30);
//            $drawing->setHeight(70);
//            $drawing->setWorksheet($delegate);
            $row++;
        }
        return $result;
    }
}
